package com.abe.bathsystem.widget.text;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.abe.bathsystem.entity.http.HttpNoticeEntity;

import net.yet.campusdev.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class LooperView extends FrameLayout {
    public interface OnChoosePosListener {
        void onChoose(int pos);
    }

    public void setListener(OnChoosePosListener listener) {
        this.listener = listener;
    }

    OnChoosePosListener listener;

    private List<HttpNoticeEntity> noticeEntities;
    private int curTipIndex = 0;
    private long lastTimeMillis;
    private static final int ANIM_DELAYED_MILLIONS = 5 * 1000;
    //动画持续时长
    private static final int ANIM_DURATION = 500;
    private View tipOut, tipIn;
    private Animation anim_out, anim_in;
    private boolean isShowRoll;

    public LooperView(Context context) {
        super(context);
        initTipFrame();
        initAnimation();
    }

    public LooperView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initTipFrame();
        initAnimation();
    }

    public LooperView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initTipFrame();
        initAnimation();
    }

    private void initTipFrame() {
        tipOut = normalView();
        tipIn = normalView();
        addView(tipIn);
        addView(tipOut);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onChoose(curTipIndex % noticeEntities.size());
                }
            }
        });
    }

    private void initAnimation() {
        anim_out = newAnimation(0, -1);
        anim_in = newAnimation(1, 0);
        anim_in.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                updateTipAndPlayAnimationWithCheck();
            }
        });
    }

    private View normalView() {
        return LayoutInflater.from(getContext()).inflate(R.layout.item_view_v3_notice, null);
    }

    private Animation newAnimation(float fromYValue, float toYValue) {
        Animation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, fromYValue, Animation.RELATIVE_TO_SELF, toYValue);
        anim.setDuration(ANIM_DURATION);
        anim.setStartOffset(ANIM_DELAYED_MILLIONS);
        anim.setInterpolator(new DecelerateInterpolator());
        return anim;
    }

    private void updateTipAndPlayAnimationWithCheck() {
        if (System.currentTimeMillis() - lastTimeMillis < 1000) {
            return;
        }
        lastTimeMillis = System.currentTimeMillis();
        updateTipAndPlayAnimation();
    }

    private void updateTipAndPlayAnimation() {
        if (!isShowRoll) return;
        if (curTipIndex % 2 == 0) {
            updateTip(tipOut);
            tipIn.startAnimation(anim_out);
            tipOut.startAnimation(anim_in);
            this.bringChildToFront(tipIn);
        } else {
            updateTip(tipIn);
            tipOut.startAnimation(anim_out);
            tipIn.startAnimation(anim_in);
            this.bringChildToFront(tipOut);
        }
    }

    private void updateTip(View tipView) {
        HttpNoticeEntity tip = getNextTip();
        if (tip != null) {
            AlwaysMarqueeTextView titleView = tipView.findViewById(R.id.item_notice_title);
            TextView timeTextView = tipView.findViewById(R.id.item_notice_time);
            titleView.setText(tip.getTitle());
            timeTextView.setText(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(new Date(tip.getLastTime())));
        }
    }

    /**
     * 获取下一条消息
     */
    private HttpNoticeEntity getNextTip() {
        if (isListEmpty(noticeEntities)) return null;
        return noticeEntities.get(curTipIndex++ % noticeEntities.size());
    }

    public static boolean isListEmpty(List list) {
        return list == null || list.isEmpty();
    }

    public void setTipList(List<HttpNoticeEntity> noticeEntities, boolean isShowRoll) {
        this.noticeEntities = noticeEntities;
        this.isShowRoll = isShowRoll;
        curTipIndex = 0;
        updateTip(tipOut);
        updateTipAndPlayAnimation();
    }
}
